package xna.android;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class Direcional {

	private float touchX, touchY;
	private Bitmap bitmap;		
	private int x;				
	private int y;
	private boolean touched;

	public Bitmap getBitmap() {
		return bitmap;
	}

	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public boolean isTouched() {
		return touched;
	}

	public void setTouched(boolean touched) {
		this.touched = touched;
	}

	public Direcional(Bitmap bitmap, int x, int y) 
	{
		this.bitmap = bitmap;
		this.x = x;
		this.y = y;
		touchX = touchY = 0;
		touched = false;
	}
	
	public void handleActionDown(int eventX, int eventY)
	{
		if(x>eventX || x+bitmap.getWidth()<eventX || y>eventY || y+bitmap.getHeight()<eventY)
		{
			touched = false;
		}
		else
		{
			touched = true;
			touchX = (eventX-x-bitmap.getWidth()/2);
			touchY = ((eventY-y-bitmap.getHeight()/2)*(-1));
		}
	}
	
	public Vector2 getDirection()
	{
		if(touched)
			if(Math.abs(touchX)>Math.abs(touchY))
			{
				if(touchX>0)
					return new Vector2(1,0);
				else
					return new Vector2(-1,0);
			}
			else 
			{
				if(touchY>0)
					return new Vector2(0,-1);
				else
					return new Vector2(0,1);
			}
		else
			return Vector2.Zero();
	}
	
	public void draw(Canvas canvas)
	{
		canvas.drawBitmap(bitmap, x, y, null);
	}
}
